package com.weiyun.controller;
import com.weiyun.common.R;
import com.weiyun.pojo.Cart;
import com.weiyun.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/cart")
@CrossOrigin
public class CartController {
    @Autowired
    private CartService cartService;
    @PostMapping("/listCart")
    public  R listCart(Cart cart){
        try {
            List<Cart> cartList = cartService.listCart(cart);
            System.out.println(cartList);
            return R.ok().data(cartList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/saveCart")
    public R saveCart(Cart cart){
        try {
            int i = cartService.saveCart(cart);
            if (i > 0) {
                return R.ok().data(1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.error().msg("购物车信息保存失败！");
    }
    @PostMapping("/updateCart")
    public R updateCart(Cart cart) {
        try {
            int i = cartService.updateCart(cart);
            if (i > 0) {
                return R.ok().data(1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.error().msg("购物车信息修改失败！");
    }
    @PostMapping("/removeCart")
    public R removeCart(Cart cart) {
        try {
            int i = cartService.removeCart(cart);
                return R.ok().data(1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}